package vip.aster.workflow.utils;

import com.alibaba.fastjson2.JSONArray;
import vip.aster.workflow.dto.FormItem;
import vip.aster.workflow.entity.WfForm;

import java.util.ArrayList;
import java.util.List;

public class DbUtil {

    public static String createTable(WfForm wfForm) {
        String formItems = wfForm.getFormItems();
        List<FormItem> fieldList = JSONArray.parseArray(formItems, FormItem.class);
        StringBuilder sb = new StringBuilder();
        sb.append("CREATE TABLE ");
        sb.append(wfForm.getCode());
        sb.append("(");
        sb.append("`id` varchar(64) NOT NULL COMMENT '主键',");
        sb.append(createColumn(fieldList));
        sb.append("`create_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',");
        sb.append("`create_by` varchar(64) DEFAULT NULL COMMENT '创建人',");
        sb.append("`update_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',");
        sb.append("`update_by` varchar(64) DEFAULT NULL COMMENT '修改人',");
        sb.append("`pro_inst_id` varchar(64) COMMENT '流程实例ID',");
        sb.append("`form_status` varchar(3) DEFAULT '0' COMMENT '启用状态',");
        sb.append("`is_deleted` varchar(3) DEFAULT '0' COMMENT '删除状态',");
        sb.append("PRIMARY KEY (id)");
        sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT=");
        sb.append("'");
        sb.append(wfForm.getFormName());
        sb.append("'");
        sb.append(";");
        return sb.toString();
    }

    /**
     * 创建列字段sql
     *
     * @param fieldList 列集合
     * @return
     */
    public static String createColumn(List<FormItem> fieldList) {
        StringBuffer sb = new StringBuffer();
        for (FormItem field : fieldList) {
            // 值类型
            String valueType = field.getValueType().toLowerCase();
            // 组件名称
            String name = field.getName().toLowerCase();
            // 数据库字段类型
            String dateType = "";
            if (valueType.equals("string")) {
                if (name.equals("textarea")) {
                    dateType = "VARCHAR(1000)";
                } else {
                    dateType = "VARCHAR(256)";
                }
            } else if (valueType.equals("long")) {
                dateType = "BIGINT(64)";
            } else if (valueType.equals("double")) {
                if (name.equals("amount")) {
                    dateType = "DECIMAL(10,2)";
                } else {
                    dateType = "DOUBLE";
                }
            } else if (valueType.equals("integer") || valueType.equals("int")) {
                dateType = "INT(10)";
            } else if (valueType.equals("boolean")) {
                dateType = "BOOLEAN";
            } else if (valueType.equals("time")) {
                dateType = "TIME";
            } else if (valueType.equals("date")) {
                dateType = "DATE";
            } else if (valueType.equals("datetime")) {
                dateType = "DATETIME";
            } else if (valueType.equals("object")) {
                dateType = "json";
            } else if (valueType.equals("file")) {
                dateType = "json";
            }
            sb.append(String.format("`%s` %s COMMENT '%s',", field.getId(), dateType, field.getTitle()));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        List<FormItem> list = new ArrayList<>();
        list.add(new FormItem("field123", "", "inputtext", "标题", "111", "string"));
        list.add(new FormItem("field456", "", "datetime", "日期", "2025-05-15", "string"));
        System.out.println(createColumn(list));
        /*WfForm wfForm = new WfForm();
        wfForm.setCode("test");
        wfForm.setFormName("测试");
        wfForm.setFormItems(JSONArray.toJSONString(list));
        String sql = createTable(wfForm);
        System.out.println(sql);*/
//        SqlRunner.db().insert(sql);
    }

    /**
     * `id` varchar(32) NOT NULL COMMENT '主键',
     * `create_time` datetime DEFAULT NULL COMMENT '创建时间',
     *   `create_by` varchar(64) DEFAULT NULL COMMENT '创建者',
     *   `update_time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
     *   `update_by` varchar(64) DEFAULT NULL COMMENT '修改者',
     *   pro_inst_id` varchar(64) COMMENT '流程实例主键',
     *   `status` varchar(1) DEFAULT '0' COMMENT '启用状态',
     *   `is_deleted` varchar(1) DEFAULT '0' COMMENT '删除状态',
     *
     *
     */


}
